import React from 'react';
import { inject, observer} from 'mobx-react';
import { WeaDialog,WeaSearchGroup,WeaFormItem,WeaRightMenu,WeaLocaleProvider } from 'ecCom';
import { WeaSwitch } from 'comsMobx';
import { Button } from 'antd';
import CofirmModal from './ConfirmModal';

const getLabel = WeaLocaleProvider.getLabel;

@inject ('wfAgentStore')

@observer

export default class Main extends React.Component{
    constructor(props){
        super(props);
    }

    handleCancel = () => {
        const { wfAgentStore } = this.props;
        wfAgentStore.batchEdit(false);
    }
    handleSave  = () => {
        const {wfAgentStore} = this.props;
        const {batchEditForm,agentid} = wfAgentStore;
        const formParams = batchEditForm.getFormParams();
        if(!wfAgentStore.verifyTime('batchEditForm')){
            return ;
        }
        let params = {...{method:"editBatch",agentids:agentid},...formParams};
        wfAgentStore.verifyAgentOverlap(params);
    }
    confirmOk = (v) => {
        const {wfAgentStore} = this.props;
        const {batchEditForm,dataSave,agentid} = wfAgentStore;
        let params = batchEditForm.getFormParams();
        params = {...params,...{method:"editBatch",agentids:agentid}};
        params["overlapMethod"] = v;
        dataSave(params);
        this.confirmCancel();
    }
    confirmCancel = () => {
        const {wfAgentStore} = this.props;
        wfAgentStore.repetitionValue = 0;
    }
    startDateChange = (obj) => {
        const {wfAgentStore} = this.props;
        wfAgentStore.startDateChange(obj,'batchEditDatas')
    }
    endDateChange = (obj) => {
        const {wfAgentStore} = this.props;
        wfAgentStore.endDateChange(obj,'batchEditDatas')
    }
    startTimeChange = (obj) =>  {
        const {wfAgentStore} = this.props;
        wfAgentStore.startTimeChange(obj,'batchEditForm');
    }
    endTimeChange = (obj) => {
        const {wfAgentStore} = this.props;
        wfAgentStore.endTimeChange(obj,'batchEditForm');
    }

    getSearchs = () => {
        const {wfAgentStore} = this.props;
        const {batchEditForm, batchEditDatas} = wfAgentStore;
        const {isFormInit} = batchEditForm;

        if (!isFormInit) {
            return;
        }

        const formParams = batchEditForm.getFormParams();

        let group = [];
        let items = [];

        items.push(
            {
                com: (
                    <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@g6ptm6@date`}
                        label={`${getLabel(740,'开始日期')},${getLabel(131410,'时间')}`}
                        labelCol={{span: 6}}
                        wrapperCol={{span: 18}}
                    >
                        <div className="wf-agent-date">
                            <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@09l42s@date`}
                                fieldConfig={batchEditDatas.beginDate}
                                form={batchEditForm}
                                formParams={formParams}
                                onChange={this.startDateChange}
                            />
                        </div>
                        <div className="wf-agent-time">
                            <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@nctfmr@time`}
                                fieldConfig={batchEditDatas.beginTime}
                                form={batchEditForm}
                                formParams={formParams}
                                onChange={this.startTimeChange}
                            />
                        </div>
                    </WeaFormItem>),
                colSpan: 1
            }, {
                com: (
                    <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@50vqi0@date`}
                        label={`${getLabel(741,'结束日期')},${getLabel(131410,'时间')}`}
                        labelCol={{span: 6}}
                        wrapperCol={{span: 18}}
                    >
                        <div className="wf-agent-date">
                            <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@idnc5l@date`}
                                fieldConfig={batchEditDatas.endDate}
                                form={batchEditForm}
                                formParams={formParams}
                                onChange={this.endDateChange}
                            />
                        </div>
                        <div className="wf-agent-time">
                            <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@s3votp@time`}
                                fieldConfig={batchEditDatas.endTime}
                                form={batchEditForm}
                                formParams={formParams}
                                onChange={this.endTimeChange}
                            />
                        </div>
                    </WeaFormItem>),
                colSpan: 1
            }
        )
        group.push(<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@k0oi4v@batcheditgroup`} col={1} className="wf-agent-batcheditgroup" needTigger={false} title={getLabel(1361,'基本信息')} showGroup={true} items={items}/>)
        return group;
    }

    getMoreBtns () {
        const { wfAgentStore } = this.props;
        const { isDisabled } = wfAgentStore;

        let btns = {
            datas: [],
            collectParams: {
                favname: getLabel(383518,'批量编辑'),
                favouritetype: 2,
                objid: 0,
                importantlevel: 1,
            }
        };

        btns.datas.push(
            {
                key: '0',
                disabled: isDisabled,
                icon: <i className="icon-coms-Preservation"/> ,
                content: getLabel(86,'保存'),
                onClick: this.handleSave,
            }
        )

        return btns;

    }

    getFooter () {
        const { wfAgentStore } = this.props;
        const { isDisabled } = wfAgentStore;

        const footer = [
            <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@c44uts@save`} disabled={isDisabled} type='primary' onClick={this.handleSave}>{getLabel(86,'保存')}</Button>,
        ];

        return footer;
    }
    render () {
        const { wfAgentStore } = this.props;
        const { batchEditVisible,repetitionValue,isDisabled } = wfAgentStore;

        return (
            <div>
                <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@1g3v0q`}
                    className="wf-agent-modal wf-agent-batchedit"
                    visible={batchEditVisible}
                    title={getLabel(383518,'批量编辑')}
                    icon="icon-coms-workflow"
                    iconBgcolor='#0079DE'
                    maskClosable={false}
                    buttons={this.getFooter()}
                    onCancel={this.handleCancel}
                    style={{width:"580px", height:"200px"}}
                    moreBtn={this.getMoreBtns()}
                >
                    <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@iev1s8`}
                        {...this.getMoreBtns()}
                    >
                        {this.getSearchs()}
                    </WeaRightMenu>
                    <CofirmModal ecId={`${this && this.props && this.props.ecId || ''}_CofirmModal@nlcqw6`}
                        visible={repetitionValue !== 0}
                        handleOk={this.confirmOk}
                        handleCancel={this.confirmCancel}
                        repetitionValue={repetitionValue}
                        method={"editBatch"}
                    />

                </WeaDialog>
            </div>
        )
    }
}